/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.texture.ManagedFileTexture;
import com.android1.amarena2d.texture.ManagedTexture;
import com.android1.amarena2d.texture.TextureDef;
import com.android1.amarena2d.texture.TextureOptions;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;

import java.util.HashMap;

public class TextureManager extends EngineObject {

    private String basePath = "";
    private HashMap<String, ManagedTexture> textures;

    public TextureManager() {
        this.textures = new HashMap<String, ManagedTexture>();
    }

    public ManagedTexture getTexture(TextureDef textureDef) {
        if (textures.containsKey(textureDef.getKey()))
            return textures.get(textureDef.getKey());
        else {
            return getTexture(textureDef.getPath(), textureDef.getFileType(), textureDef.getMinFilter(), textureDef.getMagFilter(), textureDef.getUWrap(), textureDef.getVWrap());
        }
    }

    public ManagedTexture getTexture(String path) {
        return getTexture(path, engine.getConfig().getDefaultFileType(), Texture.TextureFilter.Linear, Texture.TextureFilter.Linear, Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
    }

    public ManagedTexture getTexture(String path, TextureOptions textureOptions) {
        return getTexture(path, engine.getConfig().getDefaultFileType(), textureOptions.getMinFilter(), textureOptions.getMagFilter(), textureOptions.getUWrap(), textureOptions.getVWrap());
    }

    public ManagedTexture getTexture(String path, Files.FileType fileType, TextureOptions textureOptions) {
        return getTexture(path, fileType, textureOptions.getMinFilter(), textureOptions.getMagFilter(), textureOptions.getUWrap(), textureOptions.getVWrap());
    }

    public ManagedTexture getTexture(String path, Files.FileType fileType) {
        return getTexture(path, fileType, TextureOptions.BILINEAR);
    }

    public ManagedTexture getTexture(String path, Files.FileType fileType, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
        path = engine.getConfig().getBaseAssetsPath() + basePath + path;
        String key = TextureDef.createKey(path, fileType, minFilter, magFilter, uWrap, vWrap);
        if (textures.containsKey(key))
            return textures.get(key);
        TextureDef def = new TextureDef(path, fileType, minFilter, magFilter, uWrap, vWrap);
        ManagedTexture ref = new ManagedFileTexture(def);
        textures.put(key, ref);
        return ref;
    }

    public Pixmap getPixmap(String path, Files.FileType fileType) {
        path = engine.getConfig().getBaseAssetsPath() + basePath + path;
        return engine.getGraphics().newPixmap(engine.getFiles().getFileHandle(path, fileType));
    }

//    public ManagedTexture getTexture(Pixmap pixmap, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
//        String key = TextureDef.createKey(pixmap, minFilter, magFilter, uWrap, vWrap);
//        if (textures.containsKey(key))
//            return textures.get(pixmap);
//        TextureDef def = new TextureDef(pixmap, minFilter, magFilter, uWrap, vWrap, true);
//        ManagedTexture ref = new ManagedTexture(def);
//        textures.put(key, ref);
//        return ref;
//    }


//    public ManagedTexture createNewTexture(Pixmap pixmap, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
//        String key = TextureDef.createKey(pixmap, minFilter, magFilter, uWrap, vWrap);
//        if (textures.containsKey(key))
//            return textures.get(pixmap);
//        TextureDef def = new TextureDef(pixmap, minFilter, magFilter, uWrap, vWrap, true);
//        ManagedTexture ref = new ManagedTexture(def);
//        textures.put(key, ref);
//        return ref;
//    }


    public void remove(String key) {
        if (textures.containsKey(key)) {
            textures.remove(key);
        } else {
            engine.log("TextureManager", "Unable to remove unknown Texture: " + key);
        }
    }

    public void removeAllTextures(boolean dispose) {
        for (ManagedTexture managedTexture : textures.values()) {
            if (dispose)
                managedTexture.dispose();
        }
        textures.clear();
    }


    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }
}
